﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Util;
using System.Data.SqlClient;

namespace DAO
{
         public class News
        {
            DBConnection DBConnection;
            public News()
            {
                DBConnection = new DBConnection();
            }

            /// <summary>
            /// Get all DTO.News instance from DB.
            /// </summary>
            /// <returns>List<> of all DTO.News instances.</returns>
            public List<DTO.News> GetAllNews()
            {
                Log.LogInfo("Begin GetAllNews()");
                String query = "SELECT * FROM [News];";
                SqlDataReader reader = DBConnection.ExecuteReader(query);
                List<DTO.News> NewsDTOs = new List<DTO.News>();
                try
                {
                    while (reader.Read())
                    {
                        NewsDTOs.Add(new DTO.News(reader));
                    }
                }
                catch (Exception ex)
                {
                    Log.LogError("Error GetAllNews(): " + ex.StackTrace);
                }

                Log.LogInfo("End GetAllNews()");
                return new List<DTO.News>();
            }

            /// <summary>
            /// Get DTO.News instance from DB.
            /// </summary>
            /// <param name="ID">News ID</param>
            /// <returns>DTO.News instance which has the specific ID.</returns>
            public DTO.News GetNews(int ID)
            {
                Log.LogInfo("Begin GetNews(String ID)");
                String query = "SELECT * FROM [News] WHERE [News].[ID]='" + ID + "';";
                SqlDataReader reader = DBConnection.ExecuteReader(query);
                DTO.News newsDTO = null;
                try
                {
                    if (reader.Read())
                        newsDTO = new DTO.News(reader);
                }
                catch (Exception ex)
                {
                    Log.LogError("Error GetNews(String ID): " + ex.Message);
                }

                Log.LogInfo("End GetNews(String ID)");
                return newsDTO;
            }

            /// <summary>
            /// Get list of DTO.News by main NewsID.
            /// </summary>
            /// <param name="NewsID">ID of main News who the target newses talk about.</param>
            /// <returns>List of DTO.News instances.</returns>
            public List<DTO.News> GetNews(String NewsID)
            {
                return new List<DTO.News>();
            }

            /// <summary>
            /// Get list of DTO.News by main NewsID.
            /// </summary>
            /// <param name="NewsID">ID of main News who the target newses talk about.</param>
            /// <param name="IsRead">Whether these newses have been read or not.</param>
            /// <returns>List of DTO.News instances.</returns>
            public List<DTO.News> GetNews(String NewsID, Boolean IsRead)
            {
                return new List<DTO.News>();
            }

            /// <summary>
            /// Get list of DTO.News which have some involkes with specific News.
            /// </summary>
            /// <param name="NewsID">ID of the News who involkes with the news (The News can be News1 or News2)</param>
            /// <returns>List of DTO.News instances.</returns>
            public List<DTO.News> GetNewsHaveInvolke(String NewsID)
            {
                return new List<DTO.News>();
            }

            /// <summary>
            /// Get list of all unread DTO.News.
            /// </summary>
            /// <param name="NewsID">The main News ID.</param>
            /// <returns>List of DTO.News instances.</returns>
            public List<DTO.News> GetUnreadNews(String NewsID)
            {
                return new List<DTO.News>();
            }

            /// <summary>
            /// Insert news into database.
            /// </summary>
            /// <param name="news">DTO.News instance to insert into database.</param>
            public int InsertNews(DTO.News news)
            {
                Log.LogInfo("Begin InsertNews()");
                String query = "";
                query += "INSERT INTO NEWS (UserID_1, UserID_2, Type, Link, Symmary, Time) VALUES ('"  + news.User1ID + "','" + news.User2ID + "','" + news.Type + "','" + news.Link + news.Content + "','" + news.Time + "')";
                query += "";
                int result = DBConnection.ExecuteNonQuery(query);
                DBConnection.Close();
                Log.LogInfo("Insert " + result.ToString() + "row(s).");
                Log.LogInfo("End InsertNes()");
                return result;
            }

            /// <summary>
            /// Update News info into database.
            /// </summary>
            /// <param name="news">News instance to be updated.</param>
            public int UpdateNews(DTO.News news)
            {
                Log.LogInfo("Begin UpdateNews()");
                String query = "UPDATE NEWS SET UserID_1 = '" + news.User1ID + "'," + " UserID_2 = '" + news.User2ID + "'Type ='" + news.Type + "'Link = '" + news.Link + "'Summary = '" + news.Content + "'Time = '" + news.Time + "' WHERE ID = '" + news.ID + "'";
                int result = DBConnection.ExecuteNonQuery(query);
                DBConnection.Close();
                Log.LogInfo("Update " + result.ToString() + "row(s).");
                Log.LogInfo("End InsertNews()");
                return result;
            }

            /// <summary>
            /// Delete a specific News from DB.
            /// </summary>
            /// <param name="news">The News to be deleted.</param>
            public int DeleteNews(DTO.News news)
            {
                Log.LogInfo("Begin DeleteNews()");
                String query = "DELETE FROM [News] WHERE [News].[ID]='" + news.ID + "';";
                int result = DBConnection.ExecuteNonQuery(query);
                DBConnection.Close();
                Log.LogInfo("Deleted " + result.ToString() + "row(s).");
                Log.LogInfo("End DeleteNews()");
                return result;
            }

            /// <summary>
            /// Delete a news in database using ID.
            /// </summary>
            /// <param name="ID">News ID.</param>
            public int DeleteNews(int ID)
            {
                Log.LogInfo("Begin DeleteNews()");
                String query = "DELETE FROM [News] WHERE [News].[ID]='" + ID + "';";
                int result = DBConnection.ExecuteNonQuery(query);
                DBConnection.Close();
                Log.LogInfo("Deleted " + result.ToString() + "row(s).");
                Log.LogInfo("End DeleteNews()");
                return result;
            }
        }
}